package com.example.chirdrenserver.Utils;

import com.example.chirdrenserver.Pojo.dto.admin.AdminCreateUserDTO;
import com.example.chirdrenserver.Pojo.dto.admin.AdminUserUpdateDTO;
import com.example.chirdrenserver.Pojo.entity.User;
import com.example.chirdrenserver.Pojo.vo.admin.AdminUserVO;
import org.springframework.beans.BeanUtils;

import java.util.List;

public class AdminUserConverter {

    public static AdminUserVO toVO(User user) {
        AdminUserVO vo = new AdminUserVO();
        BeanUtils.copyProperties(user, vo);
        vo.setId(user.getId());
        if (user.getSex() != null) {
            vo.setSex(user.getSex());
            vo.setSexDisplay(user.getSex() ? "男" : "女");
        } else {
            vo.setSexDisplay("未知");
        }

        return vo;
    }

    public static AdminUserVO toVOWithRoles(User user, List<Integer> roles) {
        AdminUserVO vo = toVO(user);
        vo.setRoles(roles);
        return vo;
    }

    public static User toEntity(AdminCreateUserDTO dto) {
        User user = new User();
        BeanUtils.copyProperties(dto, user);
        return user;
    }

    public static User toEntity(AdminUserUpdateDTO dto, User user) {
        BeanUtils.copyProperties(dto, user);
        return user;
    }
}